import sys,os
sys.path.append("..")
import json,time
import urllib.request
import sys
import threading
# from 查找没用的图片 import listDir
def readAllData():
    with open('/Users/wangshuguan/江苏省全部数据.txt') as f:
        str = f.read()
        j = json.loads(str)
        return j
def getData(entpcode,certcode):
    data = {"page": 1,
            "rows": 1000}
    data_string=urllib.parse.urlencode(data)
    last_data=bytes(data_string,encoding='utf-8')
    try:
        f = urllib.request.urlopen(f"http://221.226.118.170:8080/entpcertlist/queryQualType/{entpcode}/{certcode}",data=last_data,timeout=15)
    except:
        print("超时")
        return ""
    else:
        fileContent = f.read().decode('utf-8')
        return fileContent
# data = readAllData()[0]
    
def saveDatas(start):
    datas = readAllData()
    for x in range(start,len(datas)):
        data = datas[x]
        def save(filename, contents):
                  fh = open(filename, 'w', encoding='utf-8')
                  fh.write(contents)
                  fh.close()
        ddddStr = getData(data['entpcode'],data['certcode'])
        if len(ddddStr) > 0:
            longdata = json.dumps(json.loads(ddddStr)["rows"])
            save(f'/Users/wangshuguan/江苏省企业资质/企业_{x}_{data["entpname"]}_{data["entpcode"]}.txt', longdata)
            time.sleep(1)
            print(x)
        else:
            saveDatas(x)
if __name__ == "__main__":
    saveDatas(42623)